package com.business.exam.service.impl;

import com.business.common.utils.DateUtils;
import com.business.common.utils.StringUtils;
import com.business.exam.domain.ExamQbKnownledgeLabel;
import com.business.exam.domain.ExamQbKnownledgeLabelTreeSelect;
import com.business.exam.mapper.ExamQbKnownledgeLabelMapper;
import com.business.exam.service.IExamQbKnownledgeLabelService;
import com.business.exam.utils.GeneratorSnowflake;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 知识点标签Service业务层处理
 *
 * @author zhanglele
 * @date 2023-11-17
 */
@Service
public class ExamQbKnownledgeLabelServiceImpl implements IExamQbKnownledgeLabelService
{
    @Autowired
    private ExamQbKnownledgeLabelMapper examQbKnownledgeLabelMapper;
    @Autowired
    private GeneratorSnowflake generatorSnowflake;



    /**
     * 查询知识点标签
     *
     * @param labelId 知识点标签ID
     * @return 题库分类
     */
    @Override
    public ExamQbKnownledgeLabel selectExamQbKnownledgeLabelByLabelId(Long labelId)
    {
        return examQbKnownledgeLabelMapper.selectExamQbKnownledgeLabelByLabelId(labelId);
    }
    /**
     * 查询题库分类树结构信息
     *
     * @param examQbKnownledgeLabel
     * @return 部门树信息集合
     */
    @Override
    public List<ExamQbKnownledgeLabelTreeSelect> selectKnownledgeLabelTreeList(ExamQbKnownledgeLabel examQbKnownledgeLabel)
    {
        List<ExamQbKnownledgeLabel> examQbKnownledgeLabels = examQbKnownledgeLabelMapper.selectExamQbKnownledgeLabelList(examQbKnownledgeLabel);
        return buildQbKnownledgeLabelTreeSelect(examQbKnownledgeLabels);
    }

    /**
     * 构建前端所需要树结构
     *
     * @param examQbKnownledgeLabels 知识点标签
     * @return 树结构列表
     */
    @Override
    public List<ExamQbKnownledgeLabel> buildQbKnownledgeLabelTree(List<ExamQbKnownledgeLabel> examQbKnownledgeLabels)
    {
        List<ExamQbKnownledgeLabel> returnList = new ArrayList<ExamQbKnownledgeLabel>();
        List<Long> tempList = examQbKnownledgeLabels.stream().map(ExamQbKnownledgeLabel::getLabelId).collect(Collectors.toList());
        for (ExamQbKnownledgeLabel knownledgeLabel : examQbKnownledgeLabels)
        {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(knownledgeLabel.getParentId()))
            {
                recursionFn(examQbKnownledgeLabels, knownledgeLabel);
                returnList.add(knownledgeLabel);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = examQbKnownledgeLabels;
        }
        return returnList;
    }

    /**
     * 构建前端所需要下拉树结构
     *
     * @param examQbKnownledgeLabels 知识点标签
     * @return 下拉树结构列表
     */
    @Override
    public List<ExamQbKnownledgeLabelTreeSelect> buildQbKnownledgeLabelTreeSelect(List<ExamQbKnownledgeLabel> examQbKnownledgeLabels)
    {
        List<ExamQbKnownledgeLabel> knownLedgeLabelTrees = buildQbKnownledgeLabelTree(examQbKnownledgeLabels);
        return knownLedgeLabelTrees.stream().map(ExamQbKnownledgeLabelTreeSelect::new).collect(Collectors.toList());
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<ExamQbKnownledgeLabel> list, ExamQbKnownledgeLabel t)
    {
        // 得到子节点列表
        List<ExamQbKnownledgeLabel> childList = getChildList(list, t);
        t.setChildren(childList);
        for (ExamQbKnownledgeLabel tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<ExamQbKnownledgeLabel> getChildList(List<ExamQbKnownledgeLabel> list, ExamQbKnownledgeLabel t)
    {
        List<ExamQbKnownledgeLabel> tlist = new ArrayList<ExamQbKnownledgeLabel>();
        Iterator<ExamQbKnownledgeLabel> it = list.iterator();
        while (it.hasNext())
        {
            ExamQbKnownledgeLabel n = (ExamQbKnownledgeLabel) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getLabelId().longValue())
            {
                tlist.add(n);
            }
        }
        tlist.sort((u1, u2) -> u1.getOrderNum().compareTo(u2.getOrderNum()));
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<ExamQbKnownledgeLabel> list, ExamQbKnownledgeLabel t)
    {
        return getChildList(list, t).size() > 0;
    }




    /**
     * 查询知识点标签列表
     *
     * @param examQbKnownledgeLabel 知识点标签
     * @return 知识点标签
     */
    @Override
    public List<ExamQbKnownledgeLabel> selectExamQbKnownledgeLabelList(ExamQbKnownledgeLabel examQbKnownledgeLabel)
    {
        return examQbKnownledgeLabelMapper.selectExamQbKnownledgeLabelList(examQbKnownledgeLabel);
    }

    /**
     * 新增知识点标签
     *
     * @param examQbKnownledgeLabel 知识点标签
     * @return 结果
     */
    @Override
    public int insertExamQbKnownledgeLabel(ExamQbKnownledgeLabel examQbKnownledgeLabel)
    {
        examQbKnownledgeLabel.setLabelCode(generatorSnowflake.snowflakeId());
        examQbKnownledgeLabel.setCreateTime(DateUtils.getNowDate());
        return examQbKnownledgeLabelMapper.insertExamQbKnownledgeLabel(examQbKnownledgeLabel);
    }

    /**
     * 修改知识点标签
     *
     * @param examQbKnownledgeLabel 知识点标签
     * @return 结果
     */
    @Override
    public int updateExamQbKnownledgeLabel(ExamQbKnownledgeLabel examQbKnownledgeLabel)
    {
        examQbKnownledgeLabel.setUpdateTime(DateUtils.getNowDate());
        return examQbKnownledgeLabelMapper.updateExamQbKnownledgeLabel(examQbKnownledgeLabel);
    }

    /**
     * 批量删除知识点标签
     *
     * @param labelIds 需要删除的知识点标签主键
     * @return 结果
     */
    @Override
    public int deleteExamQbKnownledgeLabelByLabelIds(Long[] labelIds)
    {
        return examQbKnownledgeLabelMapper.deleteExamQbKnownledgeLabelByLabelIds(labelIds);
    }

    /**
     * 删除知识点标签信息
     *
     * @param labelId 知识点标签主键
     * @return 结果
     */
    @Override
    public int deleteExamQbKnownledgeLabelByLabelId(Long labelId)
    {
        return examQbKnownledgeLabelMapper.deleteExamQbKnownledgeLabelByLabelId(labelId);
    }
}
